package linearList;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class SeqList<T> implements List<T> {
    private Object[] elements;
    private int length;

    public SeqList() {
        this.elements = new Object[8];
        this.length = 0;
    }

    public SeqList(T[] data) {
        this.elements = new Object[data.length];
        for(int i = 0; i < data.length; i++) {
            this.elements[i] = data[i];
        }

        this.length = data.length;
    }

    public static void main(String[] args) {
        System.out.println("--测试初始化--");
        Integer[] data = {66, 33, 15, 11, 78, 4, 6, 9, 6, 10};
        SeqList<Integer> seqList = new SeqList<Integer>(data);
        System.out.println(seqList);

        System.out.println("--测试insert(2, 22)--");
        seqList.insert(2, 22);
        System.out.println(seqList);

        System.out.println("--测试insert 10个元素--");
        for (int i = 20; i <= 30; i++) {
            seqList.insert(i);
        }
        System.out.println(seqList);

        System.out.println("--测试remove index为10的元素--");
        seqList.removeByKey(10);
        System.out.println(seqList);

        System.out.println("--测试获取 index为5的元素--");
        System.out.println(seqList.get(5));

        System.out.println("--测试设置 index为5的元素为100--");
        seqList.set(5, 100);
        System.out.println(seqList);

        System.out.println("--测试查找元素等于100的索引--");
        System.out.println(seqList.search(100));

        System.out.println("--测试remove by key--");
        seqList.removeByKey(new Integer(100));
        System.out.println(seqList);

    }

    // 实现 ToString 输出结果
    @Override
    public String toString() {
        String retstr = "";
        for(int a=0;a<this.elements.length;a++) {
            if(this.elements[a] != null) {
                retstr += this.elements[a] + "\t";
            } else {
                break;
            }
        }
        retstr += "\n";
        return retstr;
    }

    @Override
    public boolean isEmpty() {
        if(this.elements == null) {
            return true;
        }
        return false;
    }

    @Override
    public boolean contains(Object o) {
        return false;
    }

    @Override
    public Iterator<T> iterator() {
        return null;
    }

    @Override
    public Object[] toArray() {
        return new Object[0];
    }

    @Override
    public <T1> T1[] toArray(T1[] t1s) {
        return null;
    }

    @Override
    public boolean add(T t) {
        return false;
    }

    @Override
    public boolean remove(Object o) {
        return false;
    }

    @Override
    public boolean containsAll(Collection<?> collection) {
        return false;
    }

    @Override
    public boolean addAll(Collection<? extends T> collection) {
        return false;
    }

    @Override
    public boolean addAll(int i, Collection<? extends T> collection) {
        return false;
    }

    @Override
    public boolean removeAll(Collection<?> collection) {
        return false;
    }

    @Override
    public boolean retainAll(Collection<?> collection) {
        return false;
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public T get(int i) {
        for(int j = 0; j<this.elements.length; j++) {
            if(i == j) {
                return (T) new Integer(String.valueOf(this.elements[i]));
            }
        }
        // 不存在
        return null;
    }

    @Override
    public T set(int i, T x) {
        if(this.elements.length >= i) {
            this.elements[i] = x;

            return x;
        }
        // 不存在
        return null;
    }

    @Override
    public void add(int i, T t) {

    }

    public int insert(int i, T x) {

        Object[] score = this.elements;
        this.elements = new Object[score.length*2];
        for(int j=0;j<score.length;j++) {
            this.elements[j] = score[j];
        }
        int j = this.length - 1;
        for(;j>i;j--) {
            this.elements[j+1] = score[j];
        }
        this.elements[j] = x;

        return i;
    }

    public int insert(T x) {

        Object[] score = this.elements;
        this.elements = new Object[score.length + 1];
        for(int j=0;j<score.length;j++) {
            this.elements[j] = score[j];
        }
        for(int j=0;j<this.elements.length;j++) {
            if(this.elements[j] == null) {
                this.elements[j] = x;
                break;
            }
        }

        return score.length;
    }

    @Override
    public T remove(int i) {
        return null;
    }

    @Override
    public int indexOf(Object o) {
        return 0;
    }

    @Override
    public int lastIndexOf(Object o) {
        return 0;
    }

    @Override
    public ListIterator<T> listIterator() {
        return null;
    }

    @Override
    public ListIterator<T> listIterator(int i) {
        return null;
    }

    @Override
    public List<T> subList(int i, int i1) {
        return null;
    }

    @Override
    public void clear() {

    }

    public int search(T key) {

        for(int i = 0;i <this.elements.length;i++) {
            if(this.elements[i] == (Integer)key) {
                return i;
            }
        }
        return 0;
    }

    public T removeByKey(T key) {

        for(int i = (Integer) key; i<this.elements.length; i++) {
            if(this.elements[i] != null) {
                this.elements[i] = this.elements[i + 1];
            } else {
                break;
            }
        }

        return key;

    }

}
